package com.smart.community.user.controller;

import com.smart.community.user.service.UserPropertyCompanyService;
import com.smart.community.user.dto.UserPropertyCompanyDTO;
import com.smart.community.user.dto.UserPropertyCompanyQueryDTO;
import com.smart.community.user.vo.UserPropertyCompanyVO;
import com.smart.community.commons.result.PageResult;
import com.smart.community.commons.result.Result;
import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户物业公司管理控制器
 *
 * @author Wu.Liang
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/user/property-companies")
@Tag(name = "用户物业公司管理", description = "用户物业公司管理相关接口")
@Slf4j
public class UserPropertyCompanyController {

    @Autowired
    private UserPropertyCompanyService userPropertyCompanyService;

    @GetMapping
    @Operation(summary = "分页查询用户物业公司列表")
    @SaCheckPermission("user-property-company:menu")
    public Result<PageResult<UserPropertyCompanyVO>> getUserPropertyCompanyList(UserPropertyCompanyQueryDTO queryDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        PageResult<UserPropertyCompanyVO> result = userPropertyCompanyService.getUserPropertyCompanyList(queryDTO);
        return Result.success("查询成功", result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<UserPropertyCompanyVO> getUserPropertyCompany(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        UserPropertyCompanyVO result = userPropertyCompanyService.getUserPropertyCompany(id);
        return Result.success("查询成功", result);
    }

    @ApiLog(
        logTitle = "新增用户物业公司",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.ADD
    )
    @PostMapping
    @Operation(summary = "新增用户物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<Void> createUserPropertyCompany(@RequestBody @Valid UserPropertyCompanyDTO userPropertyCompanyDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        userPropertyCompanyService.createUserPropertyCompany(userPropertyCompanyDTO);
        return Result.success();
    }

    @ApiLog(
        logTitle = "更新用户物业公司",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}")
    @Operation(summary = "更新用户物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<Void> updateUserPropertyCompany(@PathVariable Long id, @RequestBody @Valid UserPropertyCompanyDTO userPropertyCompanyDTO) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        userPropertyCompanyService.updateUserPropertyCompany(id, userPropertyCompanyDTO);
        return Result.success();
    }

    @ApiLog(
        logTitle = "删除用户物业公司",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.DELETE
    )
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<Void> deleteUserPropertyCompany(@PathVariable Long id) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        userPropertyCompanyService.deleteUserPropertyCompany(id);
        return Result.success();
    }

    @ApiLog(
        logTitle = "更新用户物业公司状态",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PutMapping("/{id}/status")
    @Operation(summary = "更新用户物业公司状态")
    @SaCheckPermission("user-property-company:menu")
    public Result<Void> updateUserPropertyCompanyStatus(@PathVariable Long id, @RequestParam Integer status) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        userPropertyCompanyService.updateUserPropertyCompanyStatus(id, status);
        return Result.success();
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户关联的物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<List<UserPropertyCompanyVO>> getUserPropertyCompanies(@PathVariable Long userId) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<UserPropertyCompanyVO> result = userPropertyCompanyService.getUserPropertyCompanies(userId);
        return Result.success("查询成功", result);
    }

    @ApiLog(
        logTitle = "分配用户物业公司",
        logType = 2,
        moduleName = "用户管理",
        operationType = ApiLogOperationType.UPDATE
    )
    @PostMapping("/user/{userId}")
    @Operation(summary = "分配用户物业公司")
    @SaCheckPermission("user-property-company:menu")
    public Result<Void> assignUserPropertyCompanies(@PathVariable Long userId, @RequestBody List<Long> propertyCompanyIds) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        userPropertyCompanyService.assignUserPropertyCompanies(userId, propertyCompanyIds);
        return Result.success();
    }

    @GetMapping("/property-company/{propertyCompanyId}")
    @Operation(summary = "获取物业公司关联的用户")
    @SaCheckPermission("user-property-company:menu")
    public Result<List<UserPropertyCompanyVO>> getPropertyCompanyUsers(@PathVariable Long propertyCompanyId) throws Exception {
        // 直接调用Service，异常统一抛出，不捕获处理
        List<UserPropertyCompanyVO> result = userPropertyCompanyService.getPropertyCompanyUsers(propertyCompanyId);
        return Result.success("查询成功", result);
    }
} 